#!/bin/bash

wlr-randr --output LVDS-1 --off
wlr-randr --output HDMI-A-1 --pos 0,0
wlr-randr --output VGA-2  --pos 1920,0

# waybarc
swww init & 
fcitx5 -d --replace &
# ssh login on out network
autossh -M 4000 -NR 8822:127.0.0.1:22 root@hw.61linux.com -p 27085 &

pid_path=
is_running() {
    if [ -f "${pid_path}" ] && (( $(ps -p $(cat ${pid_path}) | wc -l) > 1 )); then
        echo 0
    else
        echo 1
    fi
}

get_net_speed() {
    a=$1
    i=0
    sufs=("B" "K" "M" "G" "P")
    # bc: judge double number
    while [ $(echo "${a} > 1024" | bc) -eq 1 ]; do
        # sed 's/^\./0./' -- 保证至少显示一个个位数
        a=$(echo "scale=1; ${a} / 1024" | bc | sed 's/^\./0./')
        i=$((i+1))
    done
    a=${a}${sufs[$i]}
    ## add length to fixed 7
    dif=$((7 - $(expr length "${a}")))
    for (( i = 0; i < dif; i++ )); do
        a=" ${a}"
    done
    echo "${2}${a}"
}

# add_len 7 "123B"
add_len() {
    ## add $2 length to fixed $1
    a=$2
    dif=$((${1} - $(expr length "${a}")))
    for (( i = 0; i < dif; i++ )); do
        a=" ${a}"
    done
    echo "${a}"
}

mkdir -p /tmp/dwl
sta_cpu=/tmp/dwl/sta_cpu
sta_mem=/tmp/dwl/sta_mem
sta_thread=/tmp/dwl/sta_thread
sta_net=/tmp/dwl/sta_net

pid_path=/tmp/dwl/pid_cpu
if [ $(is_running) != 0 ]; then
	cpu_total_prev=0
	cpu_idle_prev=0
	for((;;)); do
		# name user    nice system  idle     iowait irq    softirq steal guest   guest_nice
		# cpu0 4617100 11   2835888 60064038 52079  281777 162624  0     1953941 0
		# ---数字都是 系统启动累计到当前时间
		# user     用户态的运行时间，不包含nice值为负的进程。
		# system   核心态的运行时间。
		# nice     nice 值为负的进程所占用的CPU时间。
		# idle     除IO等待时间以外的其它等待时间。
		# iowait   IO等待时间(since 2.5.41)。
		# irq      硬中断时间(since 2.6.0-test4)。
		# softirq  软中断时间(since2.6.0-test4)。
		# stealstolen which is the time spent in otheroperating systems when running in a virtualized environment(since 2.6.11)。
		# guest       whichis the time spent running a virtual CPU  for  guest operating systems under the control ofthe Linux kernel(since 2.6.24)。
        a=(`cat /proc/stat | head -1`) # line
        b=$((a[1]+a[2]+a[3]+a[4]+a[5]+a[6]+a[7]+a[8]+a[9]+a[10])) # total
        # c: usage %
        c=$(printf "%.1f" `echo "scale=5;100-$((a[4]-cpu_idle_prev))/$((b-cpu_total_prev))*100"|bc`)
        c=$([ `echo "$c<10" | bc` -eq 1 ] && echo -n " ${c}" || echo -n "${c}")
        cpu_total_prev=${b}
        cpu_idle_prev=${a[4]}
        echo "C ${c}%" > ${sta_cpu}
        sleep 2
	done &
	echo $! > ${pid_path}
	echo cpu calc pid: $!
fi

pid_path=/tmp/dwl/pid_mem
if [ $(is_running) != 0 ]; then
	for((;;)); do
        a=(`free -m | grep Mem`)
        b=$(printf "%.2f" `echo "scale=5;${a[2]}/1024"|bc`) # memory used
        c=$(printf "%.1f" `echo "scale=5;${a[1]}/1024"|bc`) # memory all
        d=$(printf "%.1f" `echo "scale=5;${a[6]}/1024"|bc`) # memory available
        echo "M ${b}/${c}G" > ${sta_mem}
        sleep 2
	done &
	echo $! > ${pid_path}
	echo memory calc pid: $!
fi

# 计算进程 线程数
pid_path=/tmp/dwl/pid_thread
if [ $(is_running) != 0 ]; then
	for((;;)); do
		a=$(ps -aux | wc -l)
		b=$(ps -eT | wc -l)
        echo "${a}p ${b}t" > ${sta_thread}
		sleep 1
	done &
	echo $! > ${pid_path}
	echo process and thread calc pid: $!
fi

# 计算网速
pid_path=/tmp/dwl/pid_net
if [ $(is_running) != 0 ]; then
    rx_prev=0  # download
    tx_prev=0  # upload
	for((;;)); do
        a=(` ip -s link show enp12s0 | grep -A 3  "RX:"`)
        rx=${a[7]}
        tx=${a[20]}
        rx=$(get_net_speed $((rx-rx_prev)))
        tx=$(get_net_speed $((tx-tx_prev)))
        rx_prev=${a[7]}
        tx_prev=${a[20]}
	    echo "$(add_len 7 ${tx})↑ $(add_len 7 ${rx})↓" > ${sta_net}
        sleep 1
	done &
	echo $! > ${pid_path}
	echo process and thread calc pid: $!
fi

somebar_path=/tmp/dwl/somebar-$!
pid_path=/tmp/dwl/pid_status_line_loop
if [ $(is_running) != 0 ]; then
	for((;;)); do
		# prw-r--r-- is not a normal file
		if [ -e "${somebar_path}" ]; then
			# echo "status $(cat ${status_path})${ds_split}$(date '+%u %m/%d %H:%M:%S')" >> ${somebar_path}_t
			echo "status [`cat $sta_thread`] [`cat $sta_net`] [`cat $sta_cpu`] [`cat $sta_mem`]  $(date '+%u %m/%d %H:%M:%S')" > ${somebar_path}
		fi
        # echo "${somebar_path} status adfsdf" >> ${somebar_path}_t1
		sleep 1
	done &
	echo $! > ${pid_path}
	echo status line loop pid: $!
fi

~/bin/lib/somebar -s ${somebar_path}

